import QtQuick 2.0
import QtQuick.Controls 1.0

Rectangle {
    id: mainWindow
    width: 360
    height: 360
    gradient: Gradient {
        GradientStop {
            position: 0.00;
            color: "#1f7a62";
        }
        GradientStop {
            position: 1.00;
            color: "#00bbaa";
        }
    }
    signal qmlSignal
    signal connect (string ip)
    signal disconnect
    signal sendMsg (string msg)

    Column {
        spacing:1

        Row {
           Button {
               id: connectButton
               text: qsTr("Connect")
               onClicked: {
                   if (connectButton.text === qsTr("Connect")) {
                       connect(peerIp.text);
                   } else {
                       disconnect();
                   }
               }
           }

           Rectangle {
               width: 100
               height: 5
               color: "transparent"
           }

           TextEdit {
               id: peerIp
               width: 150
               text: "localhost"
           }
        }

        Rectangle {
            id: rcvdMsgContainer
            width:mainWindow.width ; height:100
            color: "transparent"
            border.color: "#0d0909"
            ScrollView {
                width: parent.width
                height: parent.height
                Text {
                    id: mainText
                    text: qsTr("Hello World")
                    wrapMode: TextEdit.Wrap
                }
            }
        }

        Rectangle {
            id: spacer
            color: "transparent"
            height: 50
            width: mainWindow.width
        }

        Rectangle {
            id: sendMsgContainer
            width: mainWindow.width
            height: 100
            color: "transparent"
            border.color: "#000000"

            TextEdit {
                id: sendTextEdit
                text: "Digite a mensagem a ser enviada!"
                width: parent.width
                height: 20
                wrapMode: TextEdit.Wrap

            }
        }

        Rectangle {
            width: 50
            height: 30
            color: "transparent"
        }

        Row {
            Button {
                id: sendButton
                text: qsTr("Send!")
                onClicked: {
                    sendMsg(sendTextEdit.text)
                }
            }
            Button {
                id: clearButton
                text: qsTr("Limpar.")
                onClicked: {
                    sendTextEdit.text = ""
                }
            }
        }
    }


    //Slots:
    function msgReceivedSlot(rcvdMsg) {
        mainText.text += rcvdMsg
    }

    function connectedSlot() {
        console.log("in QML::connectedSlot")
        connectButton.text = qsTr("Connected")
    }

    function waitingForConnSlot() {
        connectButton.text = qsTr("Waiting for connection")
    }

    function disconnectedSlot() {
        console.log("in QML::disconnectedSlot")
        connectButton.text = qsTr("Connect")
    }
}

